14. 练习:GROUP BY

GROUP BY 注意事项

现在你已经了解了 JOIN GROUP BY 和聚合函数, SQL 开始彰显真正的强大作用了。请尝试回答以下几个问题,检验下你的技能!

问题:GROUP BY

根据以下 SQL 表格信息回答以下问题。如果你遇到问题或想要对比检查你的答案,可以在下一页面的顶部找到我的答案。

一个判断难点是,何时使用某个聚合函数或其他 SQL 功能最简单。请尝试回答以下问题,看看你能否找到最简单的解决方案。

  1. 哪个 客户 (按照名称)下的订单最早?你的答案应该包含订单的 客户名称 日期

  2. 算出每个客户的总销售额(单位是 美元 )。答案应该包括两列:每个公司的订单总销售额(单位是 美元 )以及公司 名称

  3. 最近的 web_event 是通过哪个 渠道 发生的,与此 web_event 相关的 客户 是哪个?你的查询应该仅返回三个值: 日期 渠道 客户名称

  4. 算出 web_events 中每种 渠道 的次数。最终表格应该有两列: 渠道 和渠道的使用次数。

  5. 与最早的 web_event 相关的 主要联系人 是谁?

  6. 每个 客户 所下的最小订单是什么(以 总金额(美元) 为准)。答案只需两列:客户 名称 总金额(美元) 。从最小金额到最大金额排序。

  7. 算出每个区域的 销售代表 人数。最早表格应该包含两列: 区域 sales_reps 数量。从最少到最多的代表人数排序。

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: sql-evaluator
  • Opened files (when workspace is loaded): n/a